define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');
	var Edit = require('@mod/fieldEdit').base;
	var channels = pubjs.config('exchange');
	exports.sync = Edit.extend({
		init:function(config){
			config = pubjs.conf(config, {
				fields: [
					{text: LANG('渠道'), tip: LANG('选择渠道进行同步'), type: 'custom', field: 'AdxIds',
						validate: true, uri: '/project/modules/checkBoxGroup.base', config: {
						'items': channels,
						'id': 'id',
						'name': 'name'
					}},
					{text: LANG('同步开始时间'), type: 'custom', field: 'startTime',
						uri: '@base/common/date.datePickerNew',config:
						{
							showButton: false
						}, getValue: function() {
						return this.getData(true) || 0;
					}},
					{text: LANG('同步结束时间'), type: 'custom', field: 'endTime',
						uri: '@base/common/date.datePickerNew',config:
						{
							showButton: false
						}, getValue: function() {
						return this.getData(true) || 0;
					}},
				],
				Title: LANG('广告位同步:'),
				urlAdd: 'sync/spot',
			});

			this.$id = null;
			this.Super('init', arguments);
		}
		,dataChange: function(field, v) {
			this.Super('dataChange', arguments);
			if(field == 'startTime') {
				this.$.endTime.setup({min: v});
			}
		}
		,onDateChange: function(ev){
			var cs = this.$;
			switch (ev.from){
				case cs.startTime:
					cs.endTime.setup({min: ev.param});
					break;
				case cs.endTime:
					cs.startTime.setup({max: ev.param});
					break;
			}
			return false;
		}
		,validate: function (data) {
			if(this.Super('validate', arguments)) {
				if(!data || !data.AdxIds || !data.AdxIds.length) {
					pubjs.alert('渠道必选');
					return false;
				}
				
				return true;
			}
			
			return true
		}
		,save: function() {
			var self = this;
			var data = self.getData();
			if(self.validate(data)) {
				pubjs.loading.show();
				pubjs.mc.send(self.getConfig('urlAdd'), data, function (err, data) {
					pubjs.loading.hide();
					if(err) {
						return pubjs.alert(err.message);
					}
					
					var msg = "";
					if(data && data.created && data.created.length) {
						msg += "成功创建任务的渠道： " + data.created;
					}
					if(data && data.running && data.running.length) {
						msg += "\r\n以下渠道正在同步中： " + data.running + ". 创建失败";
					}
					
					if(msg) {
						pubjs.alert(msg);
					}
					
					self.reset();
				});
			}
			
			return false;
		}
	});
});